<?php
require_once(dirname(__FILE__)."/config.php");
//考虑安全原因不管是否开启游客投稿功能，都不允许用户投稿
CheckRank(0, 0);
if($cfg_mb_lit=='Y')
{
    ShowMsg("由于系统开启了精简版会员空间，你访问的功能不可用！","-1");
    exit();
}
require_once(DEDEINC."/dedetag.class.php");
require_once(DEDEINC."/userlogin.class.php");
require_once(DEDEINC."/customfields.func.php");
require_once(DEDEMEMBER."/inc/inc_catalog_options.php");
require_once(DEDEMEMBER."/inc/inc_archives_functions.php");
$channelid = isset($channelid) && is_numeric($channelid) ? $channelid : 3;
$typeid = isset($typeid) && is_numeric($typeid) ? $typeid : 0;
$menutype = 'content';
$menutype_son = 'soft_add';

require_once("qiniu/io.php");
require_once("qiniu/rs.php");
require_once("qiniu/fop.php");
$bucket = $cfg_qnbucket;
$accessKey = $cfg_qnaccesskey;
$secretKey = $cfg_qnsecretkey;
Qiniu_SetKeys($accessKey, $secretKey);
$putPolicy = new Qiniu_RS_PutPolicy($bucket);
$upToken = $putPolicy->Token(null);

/*-------------
function _ShowForm(){  }
--------------*/
if(empty($dopost))
{
    $cInfos = $dsql->GetOne("SELECT * FROM `#@__channeltype`  WHERE id='$channelid'; ");
    if(!is_array($cInfos))
    {
        ShowMsg('模型不正确', '-1');
        exit();
    }

    //如果限制了会员级别或类型，则允许游客投稿选项无效
    if($cInfos['sendrank']>0 || $cInfos['usertype']!='')
    {
        CheckRank(0, 0);
    }

    //检查会员等级和类型限制
    if($cInfos['sendrank'] > $cfg_ml->M_Rank)
    {
        $row = $dsql->GetOne("Select membername From `#@__arcrank` where rank='".$cInfos['sendrank']."' ");
        ShowMsg("对不起，需要[".$row['membername']."]才能在这个频道发布文档！","-1","0",5000);
        exit();
    }
    if($cInfos['usertype']!='' && $cInfos['usertype'] != $cfg_ml->M_MbType)
    {
        ShowMsg("对不起，需要[".$cInfos['usertype']."帐号]才能在这个频道发布文档！","-1","0",5000);
        exit();
    }
    include(DEDEMEMBER."/templets/soft_add.htm");
    exit();
}

/*------------------------------------------------------------------------------------------

									****柒码网络扩展自定义****
	
------------------------------------------------------------------------------------------*/

else if($dopost=='newsave')
{
		
		$bigfile_info=$_POST['bigfile_info'];
		$bigfile_name=$_POST['bigfile_name'];
        foreach($bigfile_info as $k=>$v)
        {			
           $truefile = $cfg_basedir.$v;
            if(strlen($v)<2 || !file_exists($truefile)) continue;
            $info = '';
            $imginfos = GetImageSize($truefile, $info);
            $litpicname = $pagestyle > 2 ? GetImageMapDD($v, $cfg_ddimg_width) : '';
            if(!$hasone && $ddisfirst==1 && $litpic=='')
            {
                 $litpic = empty($litpicname) ? GetImageMapDD($v, $cfg_ddimg_width) : $litpicname;
                 $hasone = TRUE;
            }
            $imginfo =  $bigfile_name[$k];
			$ddimg = imagecut($v);//图集增加小图
         $imgurls .= "{dede:img ddimg='$ddimg' text='$imginfo' width='".$imginfos[0]."' height='".$imginfos[1]."'} $v {/dede:img}\r\n";
        }    
	  $imgurls = addslashes($imgurls);
	
	
	//print_r($imgurls)
	
	if($title=='')
	{
		ShowMsg("请填写素材名称！", '-1');
		exit();
	}
	if($addlitpic=='')
	{
		ShowMsg("请上传素材缩略图！", '-1');
		exit();
	}
	if($_POST['pan_baidu']=='')
    {
        ShowMsg("请填写百度网盘！", '-1');
        exit();
    }
    if($_POST['pan_baidu_mima']=='')
    {
        ShowMsg("请填写网盘密码！", '-1');
        exit();
    }
	
	//检测分辨率是否选择了其它（自定义输入）
	if($resolution=='-1')
	{
		$resolution = $resolution1.' x '.$resolution2;
	}
	
	//拆分关键字
	$tags = str_replace(" "," ","$tags");
	
	//去除空格
	$pan_baidu = preg_replace("/\s/","",$pan_baidu);
	//$previewurl_u = preg_replace("/\s/","",$previewurl_u);
	
	$description = '';
    include(DEDEMEMBER.'/inc/archives_check.php');

    //生成文档ID
    $arcID = GetIndexKey($arcrank,$typeid,$sortrank,$channelid,$senddate,$mid);
    if(empty($arcID))
    {
        ShowMsg("无法获得主键，因此无法进行后续操作！","-1");
        exit();
    }
    
    //分析处理附加表数据
    $inadd_f = '';
    $inadd_v = '';
    if(!empty($dede_addonfields))
    {
        $addonfields = explode(';',$dede_addonfields);
        $inadd_f = '';
        $inadd_v = '';
        if(is_array($addonfields))
        {
            foreach($addonfields as $v)
            {
                if($v=='')
                {
                    continue;
					
                }else if($v == 'templet')
                {
                    ShowMsg("你保存的字段有误,请检查！","-1");
                    exit();    
                }
                $vs = explode(',',$v);
                if(!isset(${$vs[0]}))
                {
                    ${$vs[0]} = '';
                }
                else if($vs[1]=='htmltext'||$vs[1]=='textdata')

                //HTML文本特殊处理
                {
                    ${$vs[0]} = AnalyseHtmlBody(${$vs[0]},$description,$keywords,$vs[1]);
                }
                else
                {
                    if(!isset(${$vs[0]}))
                    {
                        ${$vs[0]} = '';
                    }
                    ${$vs[0]} = GetFieldValueA(${$vs[0]},$vs[1],$arcID);
                }
                $inadd_f .= ','.$vs[0];
                $inadd_v .= " ,'".${$vs[0]}."' ";
            }
        }
        
        // if (empty($dede_fieldshash) || $dede_fieldshash != md5($dede_addonfields.$cfg_cookie_encode))
        // {
            // showMsg('数据校验不对，程序返回', '-1');
            // exit();
        // }
        
        // 这里对前台提交的附加数据进行一次校验
        $fontiterm = PrintAutoFieldsAdd($cInfos['fieldset'],'autofield', FALSE);
		
        if ($fontiterm != $inadd_f)
        {
           // ShowMsg("提交表单同系统配置不相符,请重新提交！", "-1");
           // exit();
        }
    }

    //处理图片文档的自定义属性
    if($addlitpic!='')
    {
        $flag = 'p';
    }
    $body = HtmlReplace($body,-1);

    //保存到主表
   $inQuery = "INSERT INTO `#@__archives`(id,typeid,sortrank,flag,ismake,channel,arcrank,click,money,title,shorttitle,
color,writer,source,litpic,pubdate,senddate,mid,description,keywords)
VALUES ('$arcID','$typeid','$sortrank','$flag','$ismake','$channelid','$arcrank','0','$money','$title','$shorttitle',
'$color','$writer','$source','$addlitpic','$pubdate','$senddate','{$cfg_ml->M_ID}','$describe','$tags'); ";
    if(!$dsql->ExecuteNoneQuery($inQuery))
    {
        $gerr = $dsql->GetError();
        $dsql->ExecuteNoneQuery("Delete From `#@__arctiny` where id='$arcID' ");
        ShowMsg("把数据保存到数据库主表 `#@__archives` 时出错，请联系管理员。","javascript:;");
        exit();
    }
	
	
    //素材链接列表
    $urls = '';
	if($pan_baidu!='')
	{
		$servermsg = "百度云盘";
		$urls= "{dede:link text='$servermsg' dowpwd='$pan_baidu_mima'} $pan_baidu {/dede:link}\r\n";
	}
	
	if($softurl1!='')
    {
		$servermsg1 = "本地上传";
        $urls .= "{dede:link islocal='1' text='{$servermsg1}'} $softurl1 {/dede:link}\r\n";
    }

	$urls = $urls;
    $urls = addslashes($urls);
	$urls = "";
    $softsize = $softsize.$unit;

    //保存到附加表
    $needmoney = @intval($needmoney);
    //if($needmoney > 100) $needmoney = 100;
    $cts = $dsql->GetOne("SELECT addtable FROM `#@__channeltype` WHERE id='$channelid' ");
    $addtable = trim($cts['addtable']);
    if(empty($addtable))
    {
        $dsql->ExecuteNoneQuery("DELETE FROM `#@__archives` WHERE id='$arcID'");
        $dsql->ExecuteNoneQuery("DELETE FROM `#@__arctiny` WHERE id='$arcID'");
        ShowMsg("没找到当前模型[{$channelid}]的主表信息，无法完成操作！。","javascript:;");
        exit();
    }
     $inQuery = "INSERT INTO `$addtable`(aid,typeid,redirecturl,userip,filetype,language,softtype,accredit,
    os,softrank,officialUrl,officialDemo,softsize,imgurls,softlinks,introduce,daccess,needmoney,softurl1{$inadd_f})
    VALUES ('$arcID','$typeid','$redirecturl','$useip','$filetype','$language','$softtype','$accredit',
    '$os','$softrank','$officialUrl','$officialDemo','$softsize','$imgurls','$urls','$body','$daccess','$needmoney','$softurl1'{$inadd_v});";
    if(!$dsql->ExecuteNoneQuery($inQuery))
    {
        $gerr = $dsql->GetError();
        $dsql->ExecuteNoneQuery("DELETE FROM `#@__archives` WHERE id='$arcID'");
        $dsql->ExecuteNoneQuery("DELETE FROM `#@__arctiny` WHERE id='$arcID'");
        /*echo $inQuery;
        exit();*/
        ShowMsg("把数据保存到数据库附加表 `$addtable` 时出错，请把相关信息提交给DedeCms官方","javascript:;");
        exit();
    }

    //增加积分
    $cfg_sendarc_scores = intval($cfg_sendarc_scores);
    $dsql->ExecuteNoneQuery("UPDATE `#@__member` set scores=scores+{$cfg_sendarc_scores} WHERE mid='".$cfg_ml->M_ID."' ; ");
    //更新统计
    countArchives($channelid);
    
    //生成HTML
    InsertTags($tags,$arcID);
    $artUrl = MakeArt($arcID, TRUE);
    if($artUrl=='')
    {
        $artUrl = $cfg_phpurl."/view.php?aid=$arcID";
    }
	
	$url = "content_list.php?channelid=3";
	unset($_SESSION['bigfile_info']);
	ShowMsg("发布成功，现转向素材管理页面","$url",0,100);
	echo('<script language="JavaScript">');
	echo("location.href='$url';");
	echo('</script>');
	exit;
}

